[C] Agenda - LDE
Publicado por Enzo de Brito Ferber (última atualização em 15/09/2010)
[ Hits: 6.622 ]
Homepage: http://www.maximasonorizacao.com.br
Agenda simples com funções para inserir, remover e procurar. Uso de listas duplamente encadeadas.
http://www.vivaolinux.com.br/artigo/Linguagem-C-Listas-Duplamente-Encadeadas/
// agenda.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define MALLOC(a) (a *) malloc( sizeof( a ))
#define MAX 50
// descomentar abaixo em linux
#define fflush(stdin) __fpurge(stdin)
struct contato
{
char nome[ MAX ];
char telefone[ MAX ];
char celular[ MAX ];
char email[ MAX ];
char obs[ MAX ];
struct contato *next;
struct contato *prior;
};
// variaveis para começo e fim da lista
struct contato *head;
struct contato *last;
void inserirdados( struct contato *novo )
{
struct contato *current;
// primeiro elemento da lista
if( !head )
{
head = last = novo;
novo->next = NULL;
novo->prior = NULL;
return ;
}
current = head;
while( current )
{
// comparação....
if( strcmp( current->nome, novo->nome ) < 0 )
current = current->next;
else
{
// elemento no meio da lista
if( current->prior )
{
novo->next = current;
novo->prior = current->prior;
current->prior->next = novo;
current->prior = novo;
return ;
}
// novo primeiro elemento da lista
novo->next = current;
novo->prior = NULL;
head->prior = novo;
head = novo;
return ;
}
}
// ultimo elemento da lsta
novo->next = NULL;
novo->prior = last;
last->next = novo;
last = novo;
return ;
}
struct contato *pesquisar( char *nome )
{
struct contato *current;
current = head;
while( current )
{
if( strcmp( current->nome, nome ) == 0 ) return current;
current = current->next;
}
return NULL;
}
void removercontato( struct contato *del )
{
// unico item
if ( head == last )
{
free( head );
head = last = NULL;
return ;
}
// primeiro item
if( !del->prior )
{
head = del->next;
head->prior = NULL;
free( del );
return ;
}
// ultimo item
if( !del->next )
{
last = del->prior;
last->next = NULL;
free( del );
return ;
}
// item no meio
del->prior->next = del->next;
del->next->prior = del->prior;
free( del );
return ;
}
void interfaceinserir( void )
{
struct contato *novo = MALLOC( struct contato );
// cls - windows
// clear - linux
system( "clear" );
printf( "\tNome : " ); fflush( stdin ); scanf( "%[^\n]", novo->nome );
printf( "\tTelefone : " ); fflush( stdin ); scanf( "%[^\n]", novo->telefone );
printf( "\tCelular : " ); fflush( stdin ); scanf( "%[^\n]", novo->celular );
printf( "\tE-mail : " ); fflush( stdin ); scanf( "%[^\n]", novo->email );
printf( "\tObservacoes: " ); fflush( stdin ); scanf( "%[^\n]", novo->obs );
// inserir
inserirdados( novo );
return ;
}
void interfaceremover( void )
{
char remover[ MAX ];
struct contato *del;
// cls - windows
// clear - linux
system( "clear" );
printf( "\tNome : " ); fflush( stdin ); scanf( "%[^\n]", remover );
del = pesquisar( remover );
if( del ) removercontato( del );
}
void interfacepesquisar( void )
{
struct contato *show;
char nome[ MAX ];
// cls - windows
// clear - linux
system( "clear" );
printf( "\tNome : " ); fflush( stdin ); scanf( "%[^\n]", nome );
show = pesquisar( nome );
if ( !show ) return ;
// cls - windows
// clear - linux
system( "clear" );
puts( "\n" );
printf( "\tNome : %s\n", show->nome );
printf( "\tTelefone : %s\n", show->telefone );
printf( "\tCelular : %s\n", show->celular );
printf( "\tE-mail : %s\n", show->email );
printf( "\tObservacoes: %s\n", show->obs );
fflush( stdin ); getchar();
return ;
}
void destruirlista( void )
{
struct contato *current;
current = head;
if( !head ) return ;
while( current->next )
{
current = current->next;
free( current->prior );
}
free( last );
return ;
}
void sair( void )
{
destruirlista();
exit( EXIT_SUCCESS );
}
void listar( void )
{
struct contato *current;
// cls - windows
// clear - linux
system( "clear" );
current = head;
if( !head )
{
printf( "Lista vazia.\n");
fflush( stdin ); getchar();
}
while( current )
{
printf( "%s\n", current->nome );
current = current->next;
}
fflush( stdin ); getchar();
return ;
}
// matriz de ponteiros para função
// o uso deste recurso faz com que o uso de switch...case seja dispensável
void (* funcs[5])(void) = {interfaceinserir, interfaceremover, interfacepesquisar, listar, sair};
void interfaceprincipal( void )
{
int op;
while( 1 )
{
// cls - windows
// clear - linux
system( "clear" );
printf( "\tMENU\n\n" );
printf( "\t1. INSERIR\n" );
printf( "\t2. REMOVER\n" );
printf( "\t3. INFORMACOES\n" );
printf( "\t4. LISTAR\n" );
printf( "\t5. SAIR\n" );
printf( "\n\t$ " );
fflush( stdin ); scanf( "%d", &op );
if( op >= 1 && op <= 5 )
(*funcs[op - 1])();
}
return ;
}
int main ( void )
{
interfaceprincipal();
return ;
}
Regra de Horner para cálculo do polinômio
Algorítmo para Calcular Raiz Quadrada
Nenhum comentário foi encontrado.
Monitorando o Preço do Bitcoin ou sua Cripto Favorita em Tempo Real com um Widget Flutuante
IA Turbina o Desktop Linux enquanto distros renovam forças
Como extrair chaves TOTP 2FA a partir de QRCODE (Google Authenticator)
Como realizar um ataque de força bruta para desobrir senhas?
Como usar Gpaste no ambiente Cinnamon
Atualizando o Fedora 42 para 43
ERRO: LAZARUS 4.2 64 no Linux MINT não entra mais apos ajustar desktop... (0)
Pergunta: Meu teclado não está respondendo direito como e consertar? (2)
Secure boot, artigo interessante, nada técnico. (6)
SQLITE não quer funcionar no LINUX LMDE6 64 com Lazaruz 4.2 64bit (n... (0)









